package com.lemon.smartsafety.ui.activity.bind;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.entity.event.bind.BindComItemEvent;
import com.lemon.smartsafety.entity.event.bind.BindNewComEvent;
import com.lemon.smartsafetynew.ui.activity.base.BaseSearchFragmentActivity;
import com.lemon.smartsafety.ui.fragment.bind.BindComListFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import org.greenrobot.eventbus.Subscribe;

/**
 * Created by Cooliean on 16/4/8.
 */
public class BindCompanyListActivity extends BaseSearchFragmentActivity {

    private BindComListFragment mFragment;
    private boolean mCanNew  = false;
    private String  mKeyWord = "";

    private String mApplicationCode = null;

    @Override
    protected void getBundleArgs() {
        super.getBundleArgs();
        if (getIntent().getExtras() != null) {
            mCanNew = getIntent().getBooleanExtra(BundleKey.CAN_NEW, false);
            mApplicationCode = getIntent().getStringExtra(BundleKey.APPLICATION_CODE);
        }
        mApplicationCode = StringUtils.nullValue(mApplicationCode);
    }

    @Override
    protected void callFinish() {
        AppManager.getAppManager().finishActivity(BindCompanyListActivity.class);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            callFinish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public Fragment getFragment() {
        mFragment = BindComListFragment.newInstance(mApplicationCode, mKeyWord, mCanNew);
        return mFragment;
    }

    public Fragment getFragment(String text) {
        mFragment = BindComListFragment.newInstance(mApplicationCode, text, mCanNew);
        return mFragment;
    }


    @Override
    public int getSearchHintRes() {
        return R.string.search_common_hit;
    }

    @Override
    public boolean onQueryTextSubmit(String queryText) {
        mKeyWord = StringUtils.nullValue(queryText);
        replaceFragment(R.id.main_content, getFragment(mKeyWord));
        return super.onQueryTextSubmit(queryText);
    }


    @Override
    protected void initTitle() {
        setBarTitle("企业一览");
    }


    @Subscribe
    public void onEventMainThread(BindComItemEvent event) {//通过列表选择的对象不能修改
        if (event.getTag().equals(BindComListFragment.class.getName())) {
            Bundle bundle = new Bundle();
            bundle.putSerializable(BundleKey.COM_INFO, event.getData());
            bundle.putBoolean(BundleKey.CANT_EDIT, true);
            bundle.putString(BundleKey.APPLICATION_CODE, mApplicationCode);
            UIHelper.showActivity(this, bundle, CompanyPropInfoActivity.class);
        }
    }

    @Subscribe
    public void onEventMainThread(BindNewComEvent event) {//通过列表选择的对象可以修改
        if (event.getTag().equals(BindComListFragment.class.getName())) {
            Bundle bundle = new Bundle();
            bundle.putBoolean(BundleKey.CANT_EDIT, false);
            bundle.putString(BundleKey.APPLICATION_CODE, mApplicationCode);
            UIHelper.showActivity(this, bundle, CompanyPropInfoActivity.class);
        }
    }
}
